#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import jsonify, request
from flask_restful import Resource
from utility.odoo_manager import OdooManager, ReportOdooManager  # 操作odoo的单例
from utility.error_code import error_code  # 异常代码
from utility.message_manager import MessageBusManager
from event import event_type
from command import command
import odoorpc
import logging
from utility import tools
from utility.tools import is_checking, check_physical, nullablea_dict, parse_M2M_params, check_progress

FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)


class LaboratoryTestNewAPI(Resource):
    """
    新增实验室及辅助检查
    :request: post
    :param archivesId: 档案ID
    :param checkType: 类型 1 初检 2常规产检 7 产后42天
    :param testProject: 检测项目
            [
                {
                    "dict_type": 字典类型
                    "code": code
                    "name": name
                }
            ]
    :param xcg: 血常规
            [
                {
                    "xcgXhdb": 血红蛋白g/ml
                    "xcgXxb": 血常规-血小板x10^9/L
                    "xcgBxbjs": 白细胞计数x10^9/L
                    "xcgQt": 其他
                }
            ]
    :param ncg: 尿常规
            [
                {
                    "ncgNdb": 尿蛋白(字典)
                    "ncgNqx": 尿潜血(字典)
                    "ncgNt": 尿糖(字典)
                    "ncgNtt": 尿酮体(字典)
                    "ncgQt": 其他(字典)
                }
            ]
    :param xxzh: 血型组合
            [
                {
                    "xxztXx": 血型(字典)
                    "xxztRhyx": RH阴型(字典)
                }
            ]
    :param gsgnkfxt: 肝肾功能+空腹血糖
            [
                {
                    "gsgnKfxtXqgb": 血清谷丙转氨酶U/
                    "gsgnKfxtXqgc": 血清谷草转氨酶U/L
                    "gsgnKfxtBdb": 白蛋白
                    "gsgnKfxtZdhs": 总胆红素μmol/L
                    "gsgnKfxtJhdhs": 结合胆红素μmol/L
                    "gsgnKfxtXqjg": 血清肌酐μmol/L
                    "gsgnKfxtXnsd": 血尿素氮μmol/L
                    "gsgnKfxtXt": 血糖mmol/L
                }
            ]
    :param ygsx: 乙肝三系
            [
                {
                    "ygsxBmky": 乙型肝炎表面抗原
        
                    "ygsxBmkt": 乙型肝炎表面抗体
                    "ygsxEky": 乙型肝炎e抗原
                    "ygsxEkt": 乙型肝炎e抗体
                    "ygsxHxkt": 乙型肝炎核心抗体
                    "ygsxYgjcsj": 乙肝检查时间
                    "ygsxYgjcyz": 乙肝检查孕周
                }
            ]
    :param mysx: 免疫四项
            [
                {
                    "mysxMdxqxsy": 梅毒血清学实验
                    "mysxMdjesj": 梅毒检测时间
                    "mysxMdjcyz": 梅毒检测孕周
                    "mysxHivjcjg": HIV检测结果
                    "mysxHivjcsj": HIV检测时间
                    "mysxHivjcyz": hiv检测孕周
                    "mysxBgjcjg": 丙肝检测结果
                    "mysxBgjcsj": 丙肝检测时间
                    "mysxBgjcyz": 丙肝检测孕周
        
                }
            ]
    :param ydshjc: 阴道生化检测
            [
                {
                    "ydshYdqjd": 阴道清洁度
                    "ydshYdfmw": 阴道分泌物
                }
            ]
    :param qt: 其他
            [
                {
                    "qtBc": B超
                    "唐氏综合征": 唐氏综合征
                    "qtYqzdjcjg": 孕期子癫前期检测结果
                }
            ]
    :returns: json string
    """
    def post(self):
        try:
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            check_res = is_checking(params.get("archivesId"), str(params.get('checkType')))
            if not check_res['res']:
                return jsonify({
                    'code': 1,
                    'msg': check_res['msg'],
                    'data': None
                })
            progress_data = check_progress(params.get('archivesId'), params.get('checkType'))
            if not progress_data['data']:
                return jsonify({
                    'msg': progress_data['msg'],
                    'code': 1
                })
            # 创建实验室及辅助检查
            data_={
                #'inspection_log_id': '',
                'archives_id': params.get('archivesId'),
                'type': params.get('checkType'),
                'item_ids': parse_M2M_params(params.get('testProject')),
                'xcg_xhdb': nullablea_dict(params.get('xcg'), 'xcgXhdb'),
                'xcg_bxbjs': nullablea_dict(params.get('xcg'), 'xcgXxb'),
                'xcg_xxb': nullablea_dict(params.get('xcg'), 'xcgBxbjs'),
                'xcg_qt': nullablea_dict(params.get('xcg'), 'xcgQt'),
                'ncg_ndb': nullablea_dict(params.get('ncg'), 'ncgNdb'),
                'ncg_nt': nullablea_dict(params.get('ncg'), 'ncgNt'),
                'ncg_ntt': nullablea_dict(params.get('ncg'), 'ncgNtt'),
                'ncg_nqx': nullablea_dict(params.get('ncg'), 'ncgNqx'),
                'ncg_qt': nullablea_dict(params.get('ncg'), 'ncgQt'),
                'xxzt_xx': nullablea_dict(params.get('xxzh'), 'xxztXx'),
                'xxzt_rhyx': nullablea_dict(params.get('xxzh'), 'xxztRhyx'),
                'gsgn_kfxt_xqgb': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXqgb'),
                'gsgn_kfxt_xqgc': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXqgc'),
                'gsgn_kfxt_bdb': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtBdb'),
                'gsgn_kfxt_zdhs': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtZdhs'),
                'gsgn_kfxt_jhdhs': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtJhdhs'),
                'gsgn_kfxt_xqjg': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXqjg'),
                'gsgn_kfxt_xnsd': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXnsd'),
                'gsgn_kfxt_xt': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXt'),
                'ygsx_bmky': nullablea_dict(params.get('ygsx'), 'ygsxBmky'),
                'ygsx_bmkt': nullablea_dict(params.get('ygsx'), 'ygsxBmkt'),
                'ygsx_ekt': nullablea_dict(params.get('ygsx'), 'ygsxEky'),
                'ygsx_hxkt': nullablea_dict(params.get('ygsx'), 'ygsxHxkt'),
                'ygsx_eky': nullablea_dict(params.get('ygsx'), 'ygsxEky'),
                # 'ygsx_ygjcsj': nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'),
                'ygsx_ygjcyz': nullablea_dict(params.get('ygsx'), 'ygsxYgjcyz'),
                'mysx_mdxqxsy': nullablea_dict(params.get('mysx'), 'mysxMdxqxsy'),
                'mysx_hivjcjg': nullablea_dict(params.get('mysx'), 'mysxHivjcjg'),
                'mysx_bgjcjg': nullablea_dict(params.get('mysx'), 'mysxBgjcjg'),
                # 'mysx_mdjesj': nullablea_dict(params.get('mysx'), 'mysxMdjesj'),
                # 'mysx_hivjcsj': nullablea_dict(params.get('mysx'), 'mysxHivjcsj'),
                # 'mysx_bgjcsj': nullablea_dict(params.get('mysx'), 'mysxBgjcsj'),
                'mysx_mdjcyz': nullablea_dict(params.get('mysx'), 'mysxMdjcyz'),
                'mysx_hivjcyz': nullablea_dict(params.get('mysx'), 'mysxHivjcyz'),
                'mysx_bgjcyz': nullablea_dict(params.get('mysx'), 'mysxBgjcyz'),
                'ydsh_ydfmw': nullablea_dict(params.get('ydshjc'), 'ydshYdfmw'),
                'ydsh_ydqjd': nullablea_dict(params.get('ydshjc'), 'ydshYdqjd'),
                'qt_bc': nullablea_dict(params.get('qt'), 'qtBc'),
                'qt_tszhzsc': nullablea_dict(params.get('qt'), 'qtTszhzsc'),
                'qt_yqzdjcjg': nullablea_dict(params.get('qt'), 'qtYqzdjcjg'),
                'hospital_id': 1
            }
            if nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'):
                data_['ygsx_ygjcsj'] = nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj')
            if nullablea_dict(params.get('mysx'), 'mysxMdjesj'):
                data_['mysx_mdjesj'] = nullablea_dict(params.get('mysx'), 'mysxMdjesj')
            if nullablea_dict(params.get('mysx'), 'mysxHivjcsj'):
                data_['mysx_hivjcsj'] = nullablea_dict(params.get('mysx'), 'mysxHivjcsj')
            if nullablea_dict(params.get('mysx'), 'mysxBgjcsj'):
                data_['mysx_bgjcsj'] = nullablea_dict(params.get('mysx'), 'mysxBgjcsj')
            main_id = odoo.env['inspection.laboratory'].create(data_)
            create_event = event_type.OperationLogEvent('创建实验室及辅助检查', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            logId = None
            if check_res['logId']:
                logId = check_res['logId']
                # 更新log表
                odoo.env['inspection.log'].write(logId, {
                    'sys_id': main_id
                })
                create_event = event_type.OperationLogEvent('更新log记录', 'logid:{},sys_id{}'.format(logId,main_id))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            else:
                # 创建检查记录
                check_data_ = {
                    'archives_id': params.get('archivesId'),
                    'type': params.get('checkType'),
                    'sys_id': main_id,
                    'state': '0'
                }
                logId = odoo.env['inspection.log'].create(check_data_)
                create_event = event_type.OperationLogEvent('创建检查记录', '档案id:{},cjda_id{}'.format(params.get('archivesId'),main_id))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 更新实验室及辅助检查logid
            u_data_ = {'inspection_log_id': logId}
            odoo.env['inspection.laboratory'].write(main_id, u_data_)
            create_event = event_type.OperationLogEvent('更新实验室及辅助检查logid', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件

            check_physical(int(params.get('archivesId')), params.get('checkType'))
            return jsonify({'code': 0, 'msg': 'success', 'data': {'logId': logId,'id': main_id}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class LaboratoryTestUpdateAPI(Resource):
    """
    修改实验室及辅助检查
    :request: put
    :param archivesId: 档案ID
    :param checkType: 类型 1 初检 2常规产检 7 产后42天
    :param logId: 检查表ID
    :param testProject: 检测项目
            [
                {
                    "dict_type": 字典类型
                    "code": code
                    "name": name
                }
            ]
    :param xcg: 血常规
            [
                {
                    "xcgXhdb": 血红蛋白g/ml
                    "xcgXxb": 血常规-血小板x10^9/L
                    "xcgBxbjs": 白细胞计数x10^9/L
                    "xcgQt": 其他
                }
            ]
    :param ncg: 尿常规
            [
                {
                    "ncgNdb": 尿蛋白(字典)
                    "ncgNqx": 尿潜血(字典)
                    "ncgNt": 尿糖(字典)
                    "ncgNtt": 尿酮体(字典)
                    "ncgQt": 其他(字典)
                }
            ]
    :param xxzh: 血型组合
            [
                {
                    "xxztXx": 血型(字典)
                    "xxztRhyx": RH阴型(字典)
                }
            ]
    :param gsgnkfxt: 肝肾功能+空腹血糖
            [
                {
                    "gsgnKfxtXqgb": 血清谷丙转氨酶U/
                    "gsgnKfxtXqgc": 血清谷草转氨酶U/L
                    "gsgnKfxtBdb": 白蛋白
                    "gsgnKfxtZdhs": 总胆红素μmol/L
                    "gsgnKfxtJhdhs": 结合胆红素μmol/L
                    "gsgnKfxtXqjg": 血清肌酐μmol/L
                    "gsgnKfxtXnsd": 血尿素氮μmol/L
                    "gsgnKfxtXt": 血糖mmol/L
                }
            ]
    :param ygsx: 乙肝三系
            [
                {
                    "ygsxBmky": 乙型肝炎表面抗原
        
                    "ygsxBmkt": 乙型肝炎表面抗体
                    "ygsxEky": 乙型肝炎e抗原
                    "ygsxEkt": 乙型肝炎e抗体
                    "ygsxHxkt": 乙型肝炎核心抗体
                    "ygsxYgjcsj": 乙肝检查时间
                    "ygsxYgjcyz": 乙肝检查孕周
                }
            ]
    :param mysx: 免疫四项
            [
                {
                    "mysxMdxqxsy": 梅毒血清学实验
                    "mysxMdjesj": 梅毒检测时间
                    "mysxMdjcyz": 梅毒检测孕周
                    "mysxHivjcjg": HIV检测结果
                    "mysxHivjcsj": HIV检测时间
                    "mysxHivjcyz": hiv检测孕周
                    "mysxBgjcjg": 丙肝检测结果
                    "mysxBgjcsj": 丙肝检测时间
                    "mysxBgjcyz": 丙肝检测孕周
        
                }
            ]
    :param ydshjc: 阴道生化检测
            [
                {
                    "ydshYdqjd": 阴道清洁度
                    "ydshYdfmw": 阴道分泌物
                }
            ]
    :param qt: 其他
            [
                {
                    "qtBc": B超
                    "唐氏综合征": 唐氏综合征
                    "qtYqzdjcjg": 孕期子癫前期检测结果
                }
            ]
    :returns: json string
    """
    def put(self):
        try:
            # 修改实验室及辅助检查
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            req_params = ['archivesId','logId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            check_res = is_checking(params.get("archivesId"), params.get('checkType'))
            if not check_res['res'] and not params.get('history'):
                return jsonify({
                    'code': 1,
                    'msg': check_res['msg'],
                    'data': None
                })
            data_={
                'inspection_log_id': int(params.get('logId')),
                'archives_id': params.get('archivesId'),
                'type': params.get('checkType'),
                # 'item_ids': parse_M2M_params(params.get('testProject')),
                'item_ids': [(6, 0, params.get('testProject'))],
                'xcg_xhdb': nullablea_dict(params.get('xcg'), 'xcgXhdb'),
                'xcg_bxbjs': nullablea_dict(params.get('xcg'), 'xcgXxb'),
                'xcg_xxb': nullablea_dict(params.get('xcg'), 'xcgBxbjs'),
                'xcg_qt': nullablea_dict(params.get('xcg'), 'xcgQt'),
                'ncg_ndb': nullablea_dict(params.get('ncg'), 'ncgNdb'),
                'ncg_nt': nullablea_dict(params.get('ncg'), 'ncgNt'),
                'ncg_ntt': nullablea_dict(params.get('ncg'), 'ncgNtt'),
                'ncg_nqx': nullablea_dict(params.get('ncg'), 'ncgNqx'),
                'ncg_qt': nullablea_dict(params.get('ncg'), 'ncgQt'),
                'xxzt_xx': nullablea_dict(params.get('xxzh'), 'xxztXx'),
                'xxzt_rhyx': nullablea_dict(params.get('xxzh'), 'xxztRhyx'),
                'gsgn_kfxt_xqgb': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXqgb'),
                'gsgn_kfxt_xqgc': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXqgc'),
                'gsgn_kfxt_bdb': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtBdb'),
                'gsgn_kfxt_zdhs': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtZdhs'),
                'gsgn_kfxt_jhdhs': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtJhdhs'),
                'gsgn_kfxt_xqjg': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXqjg'),
                'gsgn_kfxt_xnsd': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXnsd'),
                'gsgn_kfxt_xt': nullablea_dict(params.get('gsgnkfxt'), 'gsgnKfxtXt'),
                'ygsx_bmky': nullablea_dict(params.get('ygsx'), 'ygsxBmky'),
                'ygsx_bmkt': nullablea_dict(params.get('ygsx'), 'ygsxBmkt'),
                'ygsx_ekt': nullablea_dict(params.get('ygsx'), 'ygsxEky'),
                'ygsx_hxkt': nullablea_dict(params.get('ygsx'), 'ygsxHxkt'),
                'ygsx_eky': nullablea_dict(params.get('ygsx'), 'ygsxEky'),
                # 'ygsx_ygjcsj': nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'),
                'ygsx_ygjcyz': nullablea_dict(params.get('ygsx'), 'ygsxYgjcyz'),
                'mysx_mdxqxsy': nullablea_dict(params.get('mysx'), 'mysxMdxqxsy'),
                'mysx_hivjcjg': nullablea_dict(params.get('mysx'), 'mysxHivjcjg'),
                'mysx_bgjcjg': nullablea_dict(params.get('mysx'), 'mysxBgjcjg'),
                # 'mysx_mdjesj': nullablea_dict(params.get('mysx'), 'mysxMdjesj'),
                # 'mysx_hivjcsj': nullablea_dict(params.get('mysx'), 'mysxHivjcsj'),
                # 'mysx_bgjcsj': nullablea_dict(params.get('mysx'), 'mysxBgjcsj'),
                'mysx_mdjcyz': nullablea_dict(params.get('mysx'), 'mysxMdjcyz'),
                'mysx_hivjcyz': nullablea_dict(params.get('mysx'), 'mysxHivjcyz'),
                'mysx_bgjcyz': nullablea_dict(params.get('mysx'), 'mysxBgjcyz'),
                'ydsh_ydfmw': nullablea_dict(params.get('ydshjc'), 'ydshYdfmw'),
                'ydsh_ydqjd': nullablea_dict(params.get('ydshjc'), 'ydshYdqjd'),
                'qt_bc': nullablea_dict(params.get('qt'), 'qtBc'),
                'qt_tszhzsc': nullablea_dict(params.get('qt'), 'qtTszhzsc'),
                'qt_yqzdjcjg': nullablea_dict(params.get('qt'), 'qtYqzdjcjg'),
                'hospital_id': 1
            }
            if nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'):
                data_['ygsx_ygjcsj'] = nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj')
            if nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'):
                data_['mysx_mdjesj'] = nullablea_dict(params.get('mysx'), 'mysxMdjesj')
            if nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'):
                data_['mysx_hivjcsj'] = nullablea_dict(params.get('mysx'), 'mysxHivjcsj')
            if nullablea_dict(params.get('ygsx'), 'ygsxYgjcsj'):
                data_['mysx_bgjcsj'] = nullablea_dict(params.get('mysx'), 'mysxBgjcsj')
            query_ = [
                ('archives_id', '=', int(params.get('archivesId'))),
                ('inspection_log_id', '=', int(params.get('logId')))
            ]
            inspection_laboratory = odoo.env['inspection.laboratory'].search(query_)
            if not inspection_laboratory:
                return jsonify({'code': 40005, 'msg': error_code[40005]})  
            odoo.env['inspection.laboratory'].write(inspection_laboratory, data_)
            create_event = event_type.OperationLogEvent('修改实验室及辅助检查', '档案id:{}'.format(params.get('archives_id')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            if not params.get('history'):
                check_physical(int(params.get('archivesId')), params.get('checkType'))
            return jsonify({'code': 0, 'msg': 'success', 'data': {'logId': params.get('logId'),'id': inspection_laboratory}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class LaboratoryTestExtResAPI(Resource):
    """
    提取实验结果 调用第三方接口获取试验结果
    :request: get
    :param archivesId: 档案ID
    :param checkType: 1 初检 2 常规 3 产后42天
    :returns: json string
    """
    def get(self):
        try:
            odooReport = ReportOdooManager().get_instance()
            odoo = OdooManager().get_instance()
            params = request.values
            # 参数校验http://140.246.134.213:8069
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            #获取身份信息
            data_ = {}
            query_ = [('id', '=', int(params.get('archivesId')))]
            res_archives = odoo.env['res.archives'].search_read(query_, ['mq_sfz'])
            if  res_archives:
                res_archives = res_archives.pop()
                mq_sfz = res_archives['mq_sfz']
                mq_order = odooReport.env['inspection.order'].query_lab_report(False, params.get('testSfz'))
                print(mq_order)
                if mq_order:
                    data_ = {
                        'testProject': [
                            {
                                'dict_type': '',
                                'code': '',
                                'name': ''
                            }
                        ],
                        'xcg': {
                            'xcgXhdb': mq_order['xcg_xhdb'],
                            'xcgXxb': mq_order['xcg_xxb'],
                            'xcgBxbjs': mq_order['xcg_bxbjs'],
                            'xcgQt': mq_order['xcg_qt']
                        },
                        'ncg': {
                            'ncgNdb': mq_order['ncg_ndb'],
                            'ncgNqx': mq_order['ncg_nqx'],
                            'ncgNt': mq_order['ncg_nt'],
                            'ncgNtt': mq_order['ncg_ntt'],
                            'ncgQt': mq_order['ncg_qt']
                        },
                        'xxzh': {
                            'xxztXx': mq_order['xxzt_xx'],
                            'xxztRhyx': mq_order['xxzt_rhyx']
                        },
                        'gsgnkfxt': {
                            'gsgnKfxtXqgb': mq_order['gsgn_kfxt_xqgb'],
                            'gsgnKfxtXqgc': mq_order['gsgn_kfxt_xqgc'],
                            'gsgnKfxtBdb': mq_order['gsgn_kfxt_bdb'],
                            'gsgnKfxtZdhs': mq_order['gsgn_kfxt_zdhs'],
                            'gsgnKfxtJhdhs': mq_order['gsgn_kfxt_jhdhs'],
                            'gsgnKfxtXqjg': mq_order['gsgn_kfxt_xqjg'],
                            'gsgnKfxtXnsd': mq_order['gsgn_kfxt_xnsd'],
                            'gsgnKfxtXt': mq_order['gsgn_kfxt_xt']
                        },
                        'ygsx': {
                            'ygsxBmky': mq_order['ygsx_bmky'],
                            'ygsxBmkt': mq_order['ygsx_bmkt'],
                            'ygsxEky': mq_order['ygsx_eky'],
                            'ygsxEkt': mq_order['ygsx_ekt'],
                            'ygsxHxkt': mq_order['ygsx_hxkt'],
                            'ygsxYgjcsj': mq_order['ygsx_ygjcsj'],
                            'ygsxYgjcyz': mq_order['ygsx_ygjcyz']
                        },
                        'mysx': {
                            'mysxMdxqxsy': mq_order['mysx_mdxqxsy'],
                            'mysxMdjesj': mq_order['mysx_mdjesj'],
                            'mysxMdjcyz': mq_order['mysx_mdjcyz'],
                            'mysxHivjcjg': mq_order['mysx_hivjcjg'],
                            'mysxHivjcsj': mq_order['mysx_hivjcsj'],
                            'mysxHivjcyz': mq_order['mysx_hivjcyz'],
                            'mysxBgjcjg': mq_order['mysx_bgjcjg'],
                            'mysxBgjcsj': mq_order['mysx_bgjcsj'],
                            'mysxBgjcyz': mq_order['mysx_bgjcyz']
                        },
                        'ydshjc': {
                            'ydshYdqjd': mq_order['ydsh_ydqjd'],
                            'ydshYdfmw': mq_order['ydsh_ydfmw']
                        },
                        'qt': {
                            'qtBc': mq_order['qt_bc'],
                            'qtTszhzsc': mq_order['qt_tszhzsc'],
                            'qtYqzdjcjg': mq_order['qt_yqzdjcjg']
                        },
                        'logId': ''
                    }
            print(data_)
            return jsonify({'code': 0, 'msg': 'success', 'data': {}})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class LaboratoryTestQueryHisAPI(Resource):
    """
    查询实验室及辅助检查回显+历史
    :request: get
    :param archivesId: 档案ID
    :param logId: 历史时需要
    :param type: NULL 不区分 0 在途 1完成
    :param checkType: 1 初检 2 常规 3 产后42天
    :returns: json string
    """
    def get(self):
        try:
            #  获取实验室及辅助检查回显+历史
            odoo = OdooManager().get_instance()
            query_ = []
            params = request.values
            if  params.get('archivesId'):
                query_.append(('archives_id', '=', int(params.get('archivesId'))))
            if params.get('checkType'):
                query_.append(('type', '=', params.get('checkType')))
            if  params.get('logId'):
                c_query_ = [
                    ('state', '=', '1'),
                    ('archives_id', '=', int(params.get('archivesId'))),
                    ('id', '=', int(params.get('logId')))
                ]
                inspection_log = odoo.env['inspection.log'].search_read(c_query_, ['sys_id'])
                if inspection_log:
                    inspection_log = inspection_log.pop()
                    query_.append(('id', '=', int(inspection_log['sys_id'][0])))
            else:
                # 查询档案在途的初检档案
                res = odoo.env['inspection.log'].search_read([
                    ('archives_id', '=', int(params.get('archivesId'))),
                    ('state', '=', '0'),
                    ('type', '=', params.get('checkType'))
                ])
                if (res):
                    res = res.pop()
                    if (res['sys_id']):
                        # 将初检档案Id append到查询条件中去
                        query_.append(('id', '=', int(res['sys_id'][0])))
                    else:
                        return {
                            'msg': 'success',
                            'code': 0,
                            'data': None
                        }
                else:
                    return {
                        'msg': 'success',
                        'code': 0,
                        'data': None
                    }
            inspection_laboratory = odoo.env['inspection.laboratory'].search_read(query_)
            data_ = {}
            if inspection_laboratory:
                inspection_laboratory = inspection_laboratory.pop()
                data_ = {
                    'testProject': inspection_laboratory['item_ids'],
                    'xcg': {
                        'xcgXhdb': inspection_laboratory['xcg_xhdb'],
                        'xcgXxb': inspection_laboratory['xcg_xxb'],
                        'xcgBxbjs': inspection_laboratory['xcg_bxbjs'],
                        'xcgQt': inspection_laboratory['xcg_qt']
                    },
                    'ncg': {
                        'ncgNdb': inspection_laboratory['ncg_ndb'],
                        'ncgNqx': inspection_laboratory['ncg_nqx'],
                        'ncgNt': inspection_laboratory['ncg_nt'],
                        'ncgNtt': inspection_laboratory['ncg_ntt'],
                        'ncgQt': inspection_laboratory['ncg_qt']
                    },
                    'xxzh': {
                        'xxztXx': inspection_laboratory['xxzt_xx'],
                        'xxztRhyx': inspection_laboratory['xxzt_rhyx']
                    },
                    'gsgnkfxt': {
                        'gsgnKfxtXqgb': inspection_laboratory['gsgn_kfxt_xqgb'],
                        'gsgnKfxtXqgc': inspection_laboratory['gsgn_kfxt_xqgc'],
                        'gsgnKfxtBdb': inspection_laboratory['gsgn_kfxt_bdb'],
                        'gsgnKfxtZdhs': inspection_laboratory['gsgn_kfxt_zdhs'],
                        'gsgnKfxtJhdhs': inspection_laboratory['gsgn_kfxt_jhdhs'],
                        'gsgnKfxtXqjg': inspection_laboratory['gsgn_kfxt_xqjg'],
                        'gsgnKfxtXnsd': inspection_laboratory['gsgn_kfxt_xnsd'],
                        'gsgnKfxtXt': inspection_laboratory['gsgn_kfxt_xt']
                    },
                    'ygsx': {
                        'ygsxBmky': inspection_laboratory['ygsx_bmky'],
                        'ygsxBmkt': inspection_laboratory['ygsx_bmkt'],
                        'ygsxEky': inspection_laboratory['ygsx_eky'],
                        'ygsxEkt': inspection_laboratory['ygsx_ekt'],
                        'ygsxHxkt': inspection_laboratory['ygsx_hxkt'],
                        'ygsxYgjcsj': inspection_laboratory['ygsx_ygjcsj'],
                        'ygsxYgjcyz': inspection_laboratory['ygsx_ygjcyz']
                    },
                    'mysx': {
                        'mysxMdxqxsy': inspection_laboratory['mysx_mdxqxsy'],
                        'mysxMdjesj': inspection_laboratory['mysx_mdjesj'],
                        'mysxMdjcyz': inspection_laboratory['mysx_mdjcyz'],
                        'mysxHivjcjg': inspection_laboratory['mysx_hivjcjg'],
                        'mysxHivjcsj': inspection_laboratory['mysx_hivjcsj'],
                        'mysxHivjcyz': inspection_laboratory['mysx_hivjcyz'],
                        'mysxBgjcjg': inspection_laboratory['mysx_bgjcjg'],
                        'mysxBgjcsj': inspection_laboratory['mysx_bgjcsj'],
                        'mysxBgjcyz': inspection_laboratory['mysx_bgjcyz']
                    },
                    'ydshjc': {
                        'ydshYdqjd': inspection_laboratory['ydsh_ydqjd'],
                        'ydshYdfmw': inspection_laboratory['ydsh_ydfmw']
                    },
                    'qt': {
                        'qtBc': inspection_laboratory['qt_bc'],
                        'qtTszhzsc': inspection_laboratory['qt_tszhzsc'],
                        'qtYqzdjcjg': inspection_laboratory['qt_yqzdjcjg']
                    },
                    'logId': inspection_laboratory['inspection_log_id'] [0]
                }
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class LaboratoryTestRecordListAPI(Resource):
    """
    获取实验室检验报告单列表
    :request: post
    :param archivesId: 档案ID
    :param type: 类型 1检验报告单 2 检查报告单
    :param isAsc：排序方向 排序的方向,示例值(asc,desc)
    :param orderByColumn：排序列
    :param pageNum：当前页数
    :param pageSize: 分页大小
    :returns: json string
    """
    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def post(self):
        try:
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            #  构造查询条件
            query_ = [('archives_id', '=', int(params.get('archivesId')))]   
            data_ = {'report':[]}
            res_archives = odoo.env['res.archives'].search_read(query_,['mq_sfz'])
            if  res_archives:
                mq_sfz = res_archives[0]['mq_sfz']
                mq_orders = odoo.env['inspection.order'].query_order(mq_sfz)     
                if mq_orders:
                    for  mq_order in mq_orders['order_list']:
                        data_tmp={
                            'reportId': '',
                            'ReportType': '',
                            'reportTime': mq_order['observation_date'],
                            'reportName': '',
                            'exameContent': '',
                            'reportUrl': mq_order['file_path'],
                            'result': '',
                            'jcjgCode': '',
                            'jcjgName': mq_orders['hospital_name']
                        }
                        data_['report'].append(data_tmp)
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})
